package com.smiier.skin;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import cn.o.app.event.listener.OnSelectedChangeListener;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.o.app.ui.OTab;
import cn.skinapp.R;

import com.evan.common.pullrefresh.PullToRefreshBase;
import com.evan.common.pullrefresh.PullToRefreshBase.OnRefreshListener;
import com.evan.common.pullrefresh.PullToRefreshListView;
import com.pifuke.adapter.TreatmentMyProjectListAdapter;
import com.smiier.skin.extra.TreatementProjectExtra;
import com.smiier.skin.net.DoctorReserveListTask;
import com.smiier.skin.net.DoctorReserveListTask.DoctorReserveItem;
import com.smiier.skin.net.DoctorReserveListTask.DoctorReserveListRequest;
import com.smiier.skin.net.DoctorReserveListTask.DoctorReserveListResponse;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.umeng.analytics.MobclickAgent;

public class MyTreatmentProjectListActivity extends BasicActivity implements
		OnRefreshListener<ListView>, OnItemClickListener {
	OTab mTab;
	PullToRefreshListView mListView;
	DoctorReserveListRequest req = new DoctorReserveListRequest();
	TreatmentMyProjectListAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_treatment);
		init();
		setNavTitle("我的项目");
		mTab = findViewById(R.id.tab, OTab.class);
		mListView = findViewById(R.id.my_treatment, PullToRefreshListView.class);
		mListView.setOnRefreshListener(this);
		mListView.setPullLoadEnabled(false);
		mListView.setPullRefreshEnabled(false);
		mListView.setScrollLoadEnabled(false);
		mListView.setOnItemClickListener(this);
		mAdapter = new TreatmentMyProjectListAdapter();
		mListView.setAdapter(mAdapter);
		mTab.setOnSelectedChangeListener(new OnSelectedChangeListener() {

			@Override
			public void onChanged(View v, int index) {
				if (index == 0) {
					req.status = null;
				} else if (index == 1) {
					req.status = "待确认";
				} else if (index == 2) {
					req.status = "已确认";
				} else if (index == 3) {
					req.status = "已消费";
				} else if (index == 4) {
					req.status = "已取消";
				}
				mAdapter.setDataProvider(null);
				mAdapter.notifyDataSetChanged();
				loadData();
			}
		});
		mListView.doPullRefreshing(true);
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		loadData();
	}

	@Override
	public void onPullUpToLoadMore(PullToRefreshBase<ListView> refreshView) {
		loadData();
	}

	void loadData() {
		DoctorReserveListTask task = new DoctorReserveListTask();
		req.userid = GlobalSettings.sUid;
		task.addListener(new NetTaskListener<DoctorReserveListTask.DoctorReserveListRequest, DoctorReserveListTask.DoctorReserveListResponse>() {

			@Override
			public void onException(
					INetTask<DoctorReserveListRequest, DoctorReserveListResponse> task,
					Exception e) {
				// e.printStackTrace();

			}

			@Override
			public void onComplete(
					INetTask<DoctorReserveListRequest, DoctorReserveListResponse> task,
					DoctorReserveListResponse response) {
				mListView.doComplete();
				if (response != null && response.size() > 0) {
					mAdapter.setDataProvider(response);
					mAdapter.notifyDataSetChanged();
					mListView.setHasMoreData(false);
					if (response.size() < 0) {
						mListView.setContextEmptyType(true, 2);
					} else {
						mListView.setContextEmptyType(false, 2);
					}
				} else {
					mListView.setContextEmptyType(true, 2);
				}
			}
		});
		task.setRequest(req);
		add(task);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		DoctorReserveItem item = mAdapter.getDataProvider().get(position);
		TreatementProjectExtra extra = new TreatementProjectExtra();
		extra.setUser(item);
		Intent i = new Intent(this, MyTreatmentProjectActivity.class);
		if (extra.putTo(i)) {
			startActivity(i);
		}
	}
	
	
/*	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
}
